 <template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['system:promotion:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['system:promotion:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['system:promotion:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
                   v-hasPermi="['system:promotion:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="promotionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="id" v-if="false"/>
      <el-table-column label="平台名称" align="center" prop="platformName" width="300"/>
      <el-table-column label="应用id" align="center" prop="appId" width="300"/>
      <el-table-column label="平台logo" align="center" width="300">
        <template slot-scope="scope">
          <a :href="scope.row.logoPath" target="_blank" v-if="scope.row.logoPath!==''">
            <img :src="scope.row.logoPath" title="点击打开" style="width: 100px;height: 60px;">
          </a>
        </template>
      </el-table-column>
      <el-table-column label="推广二维码" align="center" width="300">
        <template slot-scope="scope">
          <a :href="scope.row.qrCodePath" target="_blank" v-if="scope.row.qrCodePath!==''">
            <img :src="scope.row.qrCodePath" title="点击打开" style="width: 100px;height: 60px;">
          </a>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="200">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state == 1" style="cursor: pointer" :type="''">启用</el-tag>
          <el-tag v-if="scope.row.state == 0" style="cursor: pointer" :type="'danger'">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="应用类型" align="center" width="200">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.platformType == 1" style="cursor: pointer" :type="''">APP</el-tag>
          <el-tag v-if="scope.row.platformType == 0" style="cursor: pointer" :type="'success'">小程序</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button size="mini" type="text" icon="el-icon-edit" @click="resetQrCode(scope.row)"
                     v-hasPermi="['system:promotion:edit']">二维码生成</el-button>-->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:promotion:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:promotion:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList" />

    <!-- 添加或修改推广小程序平台对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="平台名称" prop="platformName">
          <el-input v-model="form.platformName" placeholder="请输入平台名称" size="mini"/>
        </el-form-item>
        <el-form-item label="应用id" prop="appId">
          <el-input v-model="form.appId" placeholder="请输入应用id" size="mini"/>
        </el-form-item>
        <el-form-item label="商家logo" prop="logo" style="width:95%;">
          <single-pic v-model="form.logoPath" type="image" @handleChecked="logoImg" :uploadType="8"
                      :num="1" :width="80" :height="80" />
        </el-form-item>

        <el-form-item label="推广二维码">
          <imageUpload :uploadType="21" v-model="form.qrCodePath" @handleUploadSuccess="chooseImg"  @handleRemove="removeImg"/>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state" style="width:95%;" size="mini">
            <el-radio :value="1" :label="1">禁用</el-radio>
            <el-radio :value="0" :label="2">启用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="平台类型" prop="platformType">
          <el-radio-group v-model="form.platformType" style="width:95%;" size="mini">
            <el-radio :value="1" :label="1">APP</el-radio>
            <el-radio :value="0" :label="0">小程序</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="平台地址" prop="pathUrl">
          <el-input v-model="form.pathUrl" placeholder="请输入内容" size="mini"/>
        </el-form-item>
        <el-form-item label="平台介绍" prop="introduction">
          <el-input v-model="form.introduction" type="textarea" placeholder="请输入内容" size="mini"/>
        </el-form-item>
        <el-form-item label="平台参数" prop="platformParam">
          <el-input v-model="form.platformParam" type="textarea" placeholder="请输入内容" size="mini"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listPromotion, getPromotion, delPromotion, addPromotion, updatePromotion, exportPromotion,resetQrCode} from "@/api/module/platform/promotion";
  import singlePic from "@/components/SingleMaterial";

  export default {
    name: "Promotion",
    components: { singlePic},
    data() {
      return {
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 推广小程序平台表格数据
        promotionList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      }
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询推广小程序平台列表 */
      getList() {
        this.loading = true;
        listPromotion(this.queryParams).then(response => {
          this.promotionList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          platformName: undefined,
          logo: undefined,
          logoPath:undefined,
          qrCodeUrl: undefined,
          qrCodePath:undefined,
          state: undefined,
          createTime: undefined,
          introduction: undefined,
          pathUrl:undefined,
          platformType:undefined,
          platformParam:undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length!==1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加推广小程序平台";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        const id = row.id || this.ids;
        getPromotion(id).then(response => {
          this.loading = false;
          this.form = response.data;
          if (response.data.qrCodePath=="0"||response.data.qrCodeUrl=="0"){
            this.form.qrCodePath=undefined;
            this.form.qrCodeUrl=undefined;
          }
          this.open = true;
          this.title = "修改推广小程序平台";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            if (this.form.id != null) {
              updatePromotion(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addPromotion(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        });
      },
      logoImg(data){
        console.log("logo",data);
        if(data.length > 0) {
          console.log("imgPath",data[0].imgUrl);
          console.log("url",data[0].url);
          this.form.logoPath = data[0].imgUrl;
          this.form.logo = data[0].url;
        }
      },
      chooseImg(data){
        console.log("handleUploadSuccess1 营业执照",data);
        var imgUrl = data.imgUrl[0].name;
        var imgPath = data.imgUrl[0].url;

        this.form.qrCodeUrl = imgUrl;
        this.form.qrCodePath = imgPath;
      },
      removeImg(){
        this.form.qrCodeUrl = 0;
        this.form.qrCodePath = 0;
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除推广小程序平台编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delPromotion(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /*downloadImg(row){
        window.location.href = process.env.VUE_APP_BASE_API + "/common/download/mer/" + row.id;
      },
      resetQrCode(row){
        var data = {
          id: row.id
        };
        resetQrCode(data).then(response => {
          if(response.code === 200){
            this.msgSuccess("生成成功");
            this.getList();
          }else{
            this.msgError("生成失败");
          }
        });
      },*/
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有推广小程序平台数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return; exportPromotion(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      }
    },

  };
</script>
